package cn.scs.component;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class FileReader {
    // 测试

    /*
    public static void main(String[] args) {
        String path = "storage/de531c17-1b59-431a-8cf8-f93cfa83a406";
        FileReader reader = new FileReader(path);
        System.out.println(reader.existed);
        byte[] block = reader.readOnce();
        System.out.println(block.length);
    }*/



    File file;
    boolean existed;
    FileInputStream in;
    long pointer;
    // 初始化
    public FileReader(String path) {
        file = new File(path);
        existed = file.exists();
    }
    // 初始化附加项目
    public void initFile() {
        try {
            in = new FileInputStream(file);
            pointer = 0L;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 创建
    public boolean create() {
        if (!existed) {
            try {
                file.createNewFile();   // 创建文件
                initFile();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("ERROR: File Existed!");
        }
        return false;
    }
    // 删除
    public boolean delete() {
        if (existed) {
            return file.delete();
        }
        return false;   // 否则删除失败
    }
    // 获取读取流
    public FileInputStream getIn() {
        return in;
    }
    // 分块读取 用于本地文件分块
    public byte[] readBlock(int size) {
        // 存在 且 未开启流，进行开启
        if (existed && this.in == null) {
            openStream();
        }
        byte[] block = null;
        try {
            int remain = in.available();    // 获取文件剩余字节数
            size = Math.min(size, remain);  // 分块获取size 或 remain(当remain<size)
            if (remain == 0) {
                return null;
            } else {
                block = new byte[size];  // 实例化block块
                in.read(block);
                pointer += size;    // 更新指针
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return block;   // 否则返回null
    }
    // 整体读取 用于读取块文件
    public byte[] readOnce() {
        // 文件块设置过大报错
        if (file.length() > Integer.MAX_VALUE) {
            System.out.println("ERROR: File Block size is too large! Controlled in 64MB");
            return null;
        }
        // 存在 且 未开启流，进行开启
        if (existed && this.in == null) {
            openStream();
        }
        byte[] block = null;
        try {
            block = new byte[(int) file.length()];
            if (in.available() <= block.length) {
                in.read(block);
                return block;   // 读取并返回
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    // 关闭流  (需要手动关闭)
    public void closeStream() {
        try {
            this.in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 开启流
    public void openStream() {
        initFile();
    }
}
